<?php
/**
 * Vebba PHP Framework & Website Builder
 *
 * <pre>
 * File containing functionalities for handling sessions
 * </pre>
 *
 * @package   classes.core.static
 * @version   0.7.1512 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */

/**
 * Class used for managing session data
 *
 * @package classes.core.static
 * @version 0.7.1512 alpha
 * @since   2012
 * @author  Paul Dinh
 */
class session {

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }

  /**
   * Initialise the session (used for both starting or resuming)
   */
  public static function initialise() {
    $Configs      = system::$Configs;
    $Lf_On_Server = $Configs["session"]["lifetime"]["server"];
    $Lf_Of_Cookie = $Configs["session"]["lifetime"]["cookie"];
    $Module_Name  = request::$Module;
    $Module_Path  = helper::get_script_path();

    //set ini
    ini_set("session.cookie_path","/$Module_Path");
    ini_set("session.gc_maxlifetime", $Lf_On_Server);
    ini_set("session.cookie_lifetime",$Lf_Of_Cookie);
    session_name($Module_Name."_sid");
    session_start();

    //update session cookie lifetime
    if (strlen(session_id())>0)
      setcookie(session_name(),session_id(),
      time()+$Lf_Of_Cookie,"/$Module_Path");
      
    //set some info
    self::set_value("session_name",session_name());
    self::set_value("session_id",  session_id());
    self::set_value("module_name", request::$Module);
    self::set_value("last_page",   request::$Page);
  }

  /**
   * Get all session values (the whole $_SESSION object)
   *
   * @return array The native PHP session array
   */
  public static function get_all() {
    return $_SESSION;
  }
  
  /**
   * Encode an associative array into the format save-able to session file
   *
   * @param  array $Array The associative array to encode
   * @return string       Encoded array ready to be saved to session file   
   */
  public static function encode($Array) {
    $Old_Data = $_SESSION;        //save the old data;
    $_SESSION = $Array;           //temporarily put into $_SESSION
    $Output   = session_encode(); //to use this 'session_encode' function;
    $_SESSION = $Old_Data;        //finally, make everything back to normal
    return $Output;
  }
  
  /**
   * Decode a string (session file content) to an array
   *
   * @param  string $Content The content of a session file
   * @return array           The array contain values decoded from string
   */
  public static function decode($Content) {
    $Old_Data = $_SESSION;    //save the old data;
    session_decode($Content); //convert $Content to session data
    $Output   = $_SESSION;    //get it out from $_SESSION
    $_SESSION = $Old_Data;    //make everything back to normal
    return $Output;
  }
  
  /**
   * Destroy session of current visitor
   */
  public static function destroy() {
    session_destroy();
  }

  /**
   * Get value of a certain session variable
   *
   * @param  string $Var_Name The name of session variable
   * @return mixed            The session variable
   */
  public static function get_value($Var_Name) {
    return isset($_SESSION[$Var_Name])? $_SESSION[$Var_Name]:null;
  }

  /**
   * Set a session variable
   *
   * @param string $Var_Name  The name of session variable
   * @param mixed  $Var_Value The value of session variable to set
   */
  public static function set_value($Var_Name,$Var_Value) {
    $_SESSION[$Var_Name] = $Var_Value;
  }
  
  /**
   * Clear a session variable
   *
   * @param string $Var_Name The name of session variable
   */
  public static function clear_value($Var_Name) {
    if (isset($_SESSION[$Var_Name]))
      unset($_SESSION[$Var_Name]);
  }
}

//END-OF-FILE
